package com.cap.bts.crmdp.dealer.coupon.controller;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cap.bts.commonfunc.common.util.UserUtil;
import com.cap.bts.crmdp.dealer.card.util.FileUtil;
import com.cap.bts.crmdp.dealer.coupon.constants.CouponConstants;
import com.cap.bts.crmdp.dealer.coupon.domain.CouponBatch;
import com.cap.bts.crmdp.dealer.coupon.domain.CouponBatchLine;
import com.cap.bts.crmdp.dealer.coupon.domain.CouponEligibility;
import com.cap.bts.crmdp.dealer.coupon.domain.Eligibility;
import com.cap.bts.crmdp.dealer.coupon.domain.EligibilityLine;
import com.cap.bts.crmdp.dealer.coupon.domain.Ticket;
import com.cap.bts.crmdp.dealer.coupon.domain.TicketLine;
import com.cap.bts.crmdp.dealer.coupon.domain.TicketLinePriceModel;
import com.cap.bts.crmdp.dealer.coupon.dto.CouponBatchDto;
import com.cap.bts.crmdp.dealer.coupon.dto.CouponBatchSubmitDto;
import com.cap.bts.crmdp.dealer.coupon.dto.EligibilityLineDto;
import com.cap.bts.crmdp.dealer.coupon.dto.LayerViewDto;
import com.cap.bts.crmdp.dealer.coupon.dto.ProductTypesDto;
import com.cap.bts.crmdp.dealer.coupon.dto.RedeemCouponSubmitDto;
import com.cap.bts.crmdp.dealer.coupon.dto.TicKetDefineDto;
import com.cap.bts.crmdp.dealer.coupon.dto.TicketForScopeDto;
import com.cap.bts.crmdp.dealer.coupon.dto.TicketLinePriceDto;
import com.cap.bts.crmdp.dealer.coupon.service.ICouponBatchService;
import com.cap.bts.crmdp.dealer.coupon.service.ICouponEligibilityService;
import com.cap.bts.crmdp.dealer.coupon.service.ITicKetDefineService;
import com.cap.bts.crmdp.dealer.mars.dto.AjaxResponse;
import com.cap.bts.crmdp.dealer.masterinfo.dto.OtherProductDto;
import com.cap.bts.crmdp.dealer.masterinfo.dto.ServiceInfoDefDto;
import com.cap.bts.crmdp.dealer.masterinfo.dto.TyreProductDto;
import com.cap.bts.crmdp.dealer.meal.constants.MealConstants;
import com.cap.bts.crmdp.dealer.meal.domain.MeallineModel;
import com.cap.bts.crmdp.dealer.meal.service.IMealManageService;
import com.cap.bts.crmdp.dealer.member.domain.CarPlateInfo;
import com.cap.bts.crmdp.dealer.member.dto.MemberQueryDto;
import com.cap.bts.crmdp.dealer.member.dto.MemberResultsDto;
import com.cap.bts.crmdp.dealer.member.service.IMaintainCarPlateService;
import com.cap.bts.framework.codemapping.constant.CodeMappingConstants;
import com.cap.bts.framework.codemapping.domain.CodeInfo;
import com.cap.bts.framework.codemapping.dto.CodeInfoDto;
import com.cap.bts.framework.codemapping.service.ICodeMapping;
import com.cap.bts.framework.common.constant.CommonConstants;
import com.cap.bts.framework.common.controller.BaseController;
import com.cap.bts.framework.common.exception.AppException;

/******************************************************************************
 * @File name   :      CouponController.java
 *
 * @Author      :      ANMA
 *
 * @Date        :      Jul 20, 2015
 *
 * @Copyright Notice: 
 * Copyright (c) 2014 DMS, Inc. All  Rights Reserved.
 * This software is published under the terms of the DMS Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * ----------------------------------------------------------------------------
 * Date                         Who         Version         Comments
 * Jul 20, 2015 7:03:02 PM    ANMA      1.0            Initial Version
 *****************************************************************************/

/**
 * 
 */
@Controller
@RequestMapping(value = "/crmdp/dealer/coupon/eligibility")
public class CouponEligibilityController extends BaseController {
	private static final Logger LOGGER = Logger.getLogger(CouponEligibilityController.class);
	@Autowired
	ICouponBatchService couponBatchService;

	@Autowired
	ITicKetDefineService iTicKetDefineService;

	@Autowired
	IMaintainCarPlateService maintainCarPlateService;

	@Autowired
	IMealManageService iMealManageService;

	@Autowired
	ICodeMapping iCodeMapping;

	@Autowired
	ICouponEligibilityService couponEligibilityServiceImpl;

	/**
	 * 进入适用范围编辑页面
	 */
	@RequestMapping(value = "/load/{id}")
	@ResponseBody
	public Object toEdit(@PathVariable("id")Long id, HttpServletRequest request) throws AppException {
		AjaxResponse ajaxResponse = new AjaxResponse();
		
		try{
			ajaxResponse.setData(couponEligibilityServiceImpl.get(id));
			
		}catch(Exception e) {
			LOGGER.error("", e);
			ajaxResponse.setSuccess(false);
			ajaxResponse.setMessage(e.getMessage());
		}
		
		return ajaxResponse;
	}

	/**
	 * 保存适用范围
	 * 
	 */
	@RequestMapping(value = "/save")
	@ResponseBody
	public Object save(CouponEligibility couponEligibility, HttpServletRequest request) throws AppException {
		AjaxResponse ajaxResponse = new AjaxResponse();
		
		try{
			couponEligibility.setCodeType("DealerProperty");
			
			
			couponEligibilityServiceImpl.save(couponEligibility);
			
		}catch(Exception e) {
			LOGGER.error("", e);
			ajaxResponse.setSuccess(false);
			ajaxResponse.setMessage(e.getMessage());
		}
		
		return ajaxResponse;
	}
	
	
	/**
	 * 根据Id删除使用范围
	 */
	@RequestMapping(value = "/del/{id}")
	@ResponseBody
	public Object save(@PathVariable("id")Long id, HttpServletRequest request) throws AppException {
		AjaxResponse ajaxResponse = new AjaxResponse();
		
		try{
			couponEligibilityServiceImpl.removeCouponEligibilityInfo(id);
			
		}catch(Exception e) {
			LOGGER.error("", e);
			ajaxResponse.setSuccess(false);
			ajaxResponse.setMessage(e.getMessage());
		}
		
		return ajaxResponse;
	}

}
